package com.iteaj.demo.server.handle;

import com.iteaj.demo.server.protocol.PlatformRequestClientDemoProtocol;
import com.iteaj.network.server.service.PlatformRequestHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ScheduledFuture;

/**
 * 平台主动请求客户端处理器
 */
@Component
public class PlatformRequestClientDemoHandle implements PlatformRequestHandle<PlatformRequestClientDemoProtocol>, InitializingBean {

    @Autowired
    private ThreadPoolTaskScheduler scheduler;
    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public Object business(PlatformRequestClientDemoProtocol protocol) {
        logger.warn("协议处理器 客户端响应处理 - 设备编号: {} - 状态: {}", protocol.getEquipCode(), protocol.getExecStatus());
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // 固定1分钟的周期下发指令
//        ScheduledFuture<?> future = scheduler.scheduleWithFixedDelay(() -> {
//            new PlatformRequestClientDemoProtocol("deviceSn", "123").request();
//        }, Instant.now().plusSeconds(30), Duration.ofMinutes(1));

        // 如果需要修改下发周期, 可以先取消原先的任务
        // 周期从原来的1分钟修改为3分钟
//        future.cancel(true); // 直接取消
//        scheduler.scheduleWithFixedDelay(() -> {
//            new PlatformRequestClientDemoProtocol("deviceSn", "123").request();
//        }, Instant.now().plusSeconds(30), Duration.ofMinutes(3));
    }
}
